#include "system.h"

/**
 * Include
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

// 定义一个 System 对象
System_t xtp_system;

#define STATE_LED_GPIO CONFIG_BLINK_GPIO

/**
 * 状态LED闪烁
*/
void state_led() {
    if (xtp_system.StateLed.state_led_count >= xtp_system.StateLed.state_led_ms_flag) {
        gpio_set_level(STATE_LED_GPIO, xtp_system.StateLed.bit_led_toggle_flag);
        xtp_system.StateLed.bit_led_toggle_flag ^= 1;
        xtp_system.StateLed.state_led_count = 0;
    }
}

/**
 * 初始化 System
*/
void xtp_system_init(System_t *system)
{
    /* GPIO 初始化 */
    gpio_reset_pin(STATE_LED_GPIO);
    gpio_set_direction(STATE_LED_GPIO, GPIO_MODE_OUTPUT);

    /* System 结构体初始化 */
    system->StateLed.bit_led_toggle_flag = 0;
    system->StateLed.state_led_count = 0;
	system->StateLed.state_led_ms_flag = 500;
    system->StateLed.state_led = state_led;
	system->StateLed.set_freq = set_freq;
    system->test_num = 0;
}

/**
 * 设置状态LED闪烁频率周期
*/
void set_freq(uint32_t period_ms)
{
	xtp_system.StateLed.state_led_ms_flag = period_ms;
}
